﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint.New
{
    public static class LinkedTextRangeFactory
    {
        public static LinkedTextRange CreateFromSelection()
        {
            Ppt.Selection sel = Globals.Application.ActiveWindow.Selection;

            if (sel.Type == Ppt.PpSelectionType.ppSelectionText)
            {
                if (sel.ShapeRange[1].Type == Microsoft.Office.Core.MsoShapeType.msoTable)
                {
                    Ppt.Table table = sel.ShapeRange[1].Table;
                    for (int row = 1; row <= table.Rows.Count; row++)
                        for (int col = 1; col <= table.Columns.Count; col++)
                            if (table.Cell(row, col).Selected)
                                return new LinkedTextRange(sel.TextRange, table, row, col);
                }
                else
                    return new LinkedTextRange(sel.TextRange);
            }

            return null;
        }
    }
}
